<?php


namespace app\api\controller\xilujob;


use app\common\controller\xilujob\XilujobApi;
use app\common\model\xilujob\JobInterview;
use app\common\model\xilujob\InterviewComment AS InterviewCommentModel;
use think\Exception;

/**
 * 面试评价
 */
class InterviewComment extends XilujobApi
{
    protected $noNeedLogin = ['lists'];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];

    /**
     * @ApiTitle (评论列表)
     * @ApiSummary (评论列表)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.interview_comment/lists)
     * @ApiHeaders (name=token,type=string,description="Token")
     * @ApiParams (name="job_id",type="int",require="true",description="职位id")
     */
    public function lists() {
        $job_id = $this->request->param('job_id');
        if($this->auth->id) {
            $canComment = InterviewCommentModel::where('job_id', $job_id)
                    ->where('user_id', $this->auth->id)
                    ->count() == 0;
        }else {
            $canComment = true;
        }

        $list = InterviewCommentModel::where('job_id',$job_id)
            ->with([
                'job',
                'user'=>function($query){$query->withField('id,avatar,nickname');},
//                'enterpriseinfo'=>function($query){$query->withField('id,full_name,company_name');},
            ])
            ->where('interview_comment.status',1)
            ->order('id','desc')
            ->paginate();
        $list = $list->toArray();
        $list['can_comment'] = $canComment;
        $this->success('查询成功', $list);
    }


    /**
     * @ApiTitle (待评价列表)
     * @ApiSummary (面试邀请未评价的)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.interview_comment/not_comment_list)
     * @ApiHeaders (name=token,type=string,require=true,description="Token")
     */
    public function not_comment_list() {
        $list = JobInterview::where('job_interview.status','in',[
            JobInterview::STATUS_INTERVIEW_FAILED, JobInterview::STATUS_INTERVIEW_PASSED
        ])->where('job_interview.user_id',$this->auth->id)
            ->where('has_comment',0)
            ->with([
                'job'=>function($query){$query->withField('id,job_name,city_id,town_id,salary_type,salary_min,salary_max');},
                'enterpriseinfo'=>function($query){$query->withField('id,logo,full_name,company_name');},
            ])
            ->select();
        foreach ($list as $item) {
            //fixme:with嵌套
            $item->job->city;
        }
        $this->success('查询成功', $list);
    }

    /**
     * @ApiTitle (面试评论)
     * @ApiSummary (新增面试评论)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.interview_comment/add_comment)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     * @ApiParams (name=interview_id, type=int, require=true, description="面试邀请id")
     * @ApiParams (name=environment_score, type=int, require=true, description="办公环境分数")
     * @ApiParams (name=benefit_score, type=int, require=true, description="企业福利分数")
     * @ApiParams (name=opportunity_score, type=int, require=true, description="晋升空间分数")
     * @ApiParams (name=tags, type=string, description="标签")
     * @ApiParams (name=content, type=string, description="评价内容")
     */
    public function add_comment() {
        $params = $this->request->post('',null,'trim');
        $params['user_id'] = $this->auth->id;
        try {
            $ret = InterviewCommentModel::addComment($params);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('评论成功', $ret);

    }

    /**
     * @ApiTitle (我发布的评论)
     * @ApiSummary (我发布的评论)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.interview_comment/my_comment)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function my_comment() {
        $list = InterviewCommentModel::where('interview_comment.user_id', $this->auth->id)
            ->with([
                'user'=>function($query){$query->withField('id,avatar,nickname');},
                'job'=>function($query){$query->withField('id,job_name,status');},
            ])
            ->order('id','desc')
            ->paginate();
        $this->success('查询成功',$list);
    }

}